package com.ai.stringAl;


//空格替换
public class SpaceReplacement {

    //api的方法
    public static String replaceSpaces(String str) {
        return str.replaceAll(" ", "%20");
    }
    //固定方法
    public static String replaceSpaces2(String str) {
        StringBuffer result = new StringBuffer();
        int length = str.length();
        for (int i = 0; i < length; i++) {
            char c = str.charAt(i);
            if (String.valueOf(c).equals(" ")) {
                result.append("%20");
            }else {
                result.append(c);
            }
        }
        return result.toString();
    }

    public static void main(String[] args) {
        String str = "hello world";
        System.out.println(replaceSpaces2(str));
    }
}
